package com.wu.creditmanager.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.widget.ImageView;

public class ImageUtil {
	public static void setImage(String iconFileName, ImageView image) {
		if (iconFileName != null) {
			File iconFile = getIconFile(iconFileName);
			if (iconFile.exists()) {
				FileInputStream fileInputStream = null;
				try {
					fileInputStream = new FileInputStream(iconFile);
					Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
					image.setImageBitmap(bitmap);
				} catch (FileNotFoundException e) {
				} finally {
					if (fileInputStream != null) {
						try {
							fileInputStream.close();
						} catch (IOException e) {
						}
					}
				}
			}
		}
	}
	
	public static Drawable getDrawableFile(String fileName){
		if (fileName != null) {
			File iconFile = getIconFile(fileName);
			if (iconFile.exists()) {
				FileInputStream fileInputStream = null;
				try {
					fileInputStream = new FileInputStream(iconFile);
					return new BitmapDrawable(fileInputStream);
				} catch (FileNotFoundException e) {
				} finally {
					if (fileInputStream != null) {
						try {
							fileInputStream.close();
						} catch (IOException e) {
						}
					}
				}
			}
		}
		return null;
	}

	public static File getTempFile(String tempFileName) {
		String dir = Environment.getExternalStorageDirectory()
				+ Constants.TEMP_DIR;
		File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		return new File(file, tempFileName);
	}

	public static File getIconFile(String fileName) {
		String dir = Environment.getExternalStorageDirectory()
				+ Constants.ICON_DIR;
		File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		return new File(file, fileName);
	}

}
